package com.uwlaser.rest;

import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DeviceFile;
import com.uwlaser.service.DeviceFileService;
import com.uwlaser.service.dto.DeviceFileDto;
import com.uwlaser.service.dto.DeviceFileQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "设备文件管理")
@RequestMapping("/api/deviceFile")
public class DeviceFileController {

    private final DeviceFileService deviceFileService;

    @GetMapping
    @Log("查询设备文件")
    @ApiOperation("查询设备文件")
    @PreAuthorize("@el.check('deviceFile:list','deviceMain:list')")
    public ResponseEntity query(DeviceFileQueryParam query, Pageable pageable){
        return new ResponseEntity<>(deviceFileService.queryAll(query,pageable),HttpStatus.OK);
    }

    @ApiOperation("上传文件")
    @PostMapping(value = "/upload")
    @PreAuthorize("@el.check('deviceFile:add')")
    public ResponseEntity<Object> upload(@RequestParam MultipartFile file, DeviceFileDto resources){
        if(file == null){
            throw new RuntimeException("上传文件不能为空！");
        }
        if(resources.getDeviceId() == null){
            throw new RuntimeException("设备ID不能为空！");
        }
        return new ResponseEntity<>(deviceFileService.insert(file ,resources), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增设备文件")
    @ApiOperation("新增设备文件")
    @PreAuthorize("@el.check('deviceFile:add')")
    public ResponseEntity create(@Validated @RequestBody DeviceFileDto resources){
        return new ResponseEntity<>(deviceFileService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改设备文件")
    @ApiOperation("修改设备文件")
    @PreAuthorize("@el.check('deviceFile:edit')")
    public ResponseEntity update(@Validated @RequestBody DeviceFileDto resources){
        deviceFileService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除设备文件")
    @ApiOperation("删除设备文件")
    @PreAuthorize("@el.check('deviceFile:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        deviceFileService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出设备文件")
    @ApiOperation("导出设备文件")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('deviceFile:list')")
    public void download(HttpServletResponse response, DeviceFileQueryParam query) throws IOException {
        deviceFileService.download(deviceFileService.queryAll(query), response);
    }*/

}
